using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealSoftDotNet;
using System.Text;
using System.Xml;
using System.Data.SqlClient;
using System.IO;

public partial class EmployeeList : System.Web.UI.Page
{
    private string Encryptdata(string password)
    {
        string strmsg = string.Empty;
        byte[] encode = new byte[password.Length];
        encode = System.Text.Encoding.UTF8.GetBytes(password);
        strmsg = Convert.ToBase64String(encode);
        return strmsg;
    }

    private string Decryptdata(string encryptpwd)
    {
        string decryptpwd = string.Empty;
        UTF8Encoding encodepwd = new UTF8Encoding();
        Decoder Decode = encodepwd.GetDecoder();
        byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
        int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
        char[] decoded_char = new char[charCount];
        Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
        decryptpwd = new String(decoded_char);
        return decryptpwd;
    }

    void checklic()
    {
        try
        {


            try
            {
                Database.ExecuteText(Session["con"].ToString(), "Alter table  config Add    Totalemployee varchar(200)");
                Database.ExecuteText(Session["con"].ToString(), "update dbo.Config  set  Totalemployee='" + Encryptdata("500") + "'");
            }

            catch (Exception sdssf) { }
            DataTable dt = Database.ExecuteTextDataTable(Session["con"].ToString(), "select  * from  config  where ID=(select MAX(Id) from config)");

            if (dt.Rows.Count > 0)
            {

                DateTime d1, d2;
                Session["users"] = Decryptdata(dt.Rows[0]["Totalemployee"].ToString()).Trim();
                d1 = DateTime.Parse(Decryptdata(dt.Rows[0]["FDate"].ToString()));
                d2 = System.DateTime.Now;
                int day = d2.Subtract(d1).Days;
                if (day <= 365)
                {
                    //  Lblsub.Text = day.ToString() + "   Days Remaining  for " + Session["users"].ToString() + "  Employee";
                }
                else
                {
                    Response.Redirect("welcome.aspx");
                }

            }
            else
            {
                Response.Redirect("Licence.aspx");
            }


            //TxtLogin.Text = "Admin";
            //TxtPassword.Text = "admin";
        }

        catch (Exception ee)
        {



        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        if (!IsPostBack)
        {
            try
            {
                Database.ExecuteTextDataTable(HttpContext.Current.Session["con"].ToString(), " Alter table mst_employee   Add  WeekTimeZone5 int  ");
            }
            catch (Exception)
            { }


            try
            {
                try
                {
                    SqlConnectionStringBuilder SSB11 = new SqlConnectionStringBuilder(HttpContext.Current.Session["con"].ToString());


                    String Catalog11 = SSB11.InitialCatalog;

                    var folder = Server.MapPath("~/GPSPUNCH/" + Catalog11);
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }



                    // Create Dir

                }



                catch (Exception) { }


                DataTable dts = Database.ExecuteTextDataTable(HttpContext.Current.Session["con"].ToString(), " select empcode,ProfilePic from mst_employee  where ProfilePic is not null  ");
                if (dts.Rows.Count > 0)
                {
                    for (int i = 0; i <= dts.Rows.Count - 1; i++)
                    {
                        try
                        {
                            Byte[] img = (Byte[])dts.Rows[i]["ProfilePic"];


                            SqlConnectionStringBuilder SSB = new SqlConnectionStringBuilder(HttpContext.Current.Session["con"].ToString());


                            String Catalog = SSB.InitialCatalog;


                            string filePath = Server.MapPath("~/UserPhoto/" + Catalog + "/" + dts.Rows[i]["empcode"].ToString().Trim() + ".Jpg");
                            File.WriteAllBytes(filePath, img);


                            //  string base64string1 = Convert.ToBase64String(img, 0, img.Length);


                            string paths = "~/UserPhoto/" + Catalog + "/" + dts.Rows[i]["empcode"].ToString().Trim() + ".Jpg";
                        }

                        catch (Exception)
                        { }
                        //  Database.ExecuteText(HttpContext.Current.Session["con"].ToString(), "update Tran_ManualPunch_loc  set   ImeiNo=1,imagepath='" + paths + "' where  id = " + dts.Rows[i]["id"].ToString().Trim() + "");
                        Database.ExecuteText(HttpContext.Current.Session["con"].ToString(), "update mst_employee  set  ProfilePic=null  where  empcode = '" + dts.Rows[i]["empcode"].ToString().Trim() + "'");

                    }


                }


            }
            catch (Exception ed4) { }

            if (Database.menutype == "H")
            {
                //// fileabcs


                Employee1234.Attributes.Add("class", "container-fluid");
                cardheadergreen.Attributes.Add("class", "card-header card-header-warning card-header-green");
            }

            else
            {
                Employee1234.Attributes.Add("class", "container");
                cardheadergreen.Attributes.Add("class", "card-header card-header-warning card-header-green");
            }

            //if (Session["language"] == "English")
            //{
            //    lblListofAllEmployee.Text = "List of All Employee";
            //    CheckBox1.Text = "Select All/Un-Select ALL";
            //    BtnAdd.Text = "Add";
            //    BtnDel.Text = "Delete";
            //    BtnImportFromExcel.Text = "Import from Excel";
            //    Button1.Text = "Close";
            //    Button2.Text = "Export Excel";
            //    Grid1.Columns[0].HeaderText = "Select";
            //    Grid1.Columns[1].HeaderText = "Employee Name";
            //    Grid1.Columns[2].HeaderText = "Card No.";
            //    Grid1.Columns[3].HeaderText = "Emp. Code";
            //    Grid1.Columns[4].HeaderText = "Dept. Name";
            //    Grid1.Columns[5].HeaderText = "Designation Name";
            //    Grid1.Columns[6].HeaderText = "Password";
            //    //Grid1.Columns[7].HeaderText = "Branch";
            //}
            if (Session["language"] != null)
            {
                string filename = Session["language"].ToString().Trim();
                XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("~/App_Data/" + filename + ".xml"));
                //reading the xml data  
                DataSet ds = new DataSet();
                ds.ReadXml(xmlreader);
                xmlreader.Close();
                if (ds.Tables.Count != 0)
                {
                    lblListofAllEmployee.Text = ds.Tables["EmployeeList"].Rows[0]["lblListofAllEmployee"].ToString();
                    CheckBox1.Text = ds.Tables["EmployeeList"].Rows[0]["SelectAllUn-SelectALL"].ToString();
                    BtnAdd.Text = ds.Tables["EmployeeList"].Rows[0]["cmdadd"].ToString();
                    BtnDel.Text = ds.Tables["EmployeeList"].Rows[0]["cmddelete"].ToString();
                    BtnImportFromExcel.Text = ds.Tables["EmployeeList"].Rows[0]["ImportfromExcel"].ToString();
                    Button1.Text = ds.Tables["EmployeeList"].Rows[0]["cmdclose"].ToString();
                    Button2.Text = ds.Tables["EmployeeList"].Rows[0]["cmdexportexcel"].ToString();
                    Grid1.Columns[0].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns0"].ToString();
                    Grid1.Columns[1].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns1"].ToString();
                    Grid1.Columns[2].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns2"].ToString();
                    Grid1.Columns[3].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns3"].ToString();
                    Grid1.Columns[4].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns4"].ToString();
                    Grid1.Columns[5].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns5"].ToString();
                    Grid1.Columns[6].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns6"].ToString();
                    //Grid1.Columns[7].HeaderText = "Branch";
                }
            }
            // if (Session["language"] == "Danish")
            //{
            //    XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("~/App_Data/Danish.xml"));
            //    //reading the xml data  
            //    DataSet ds = new DataSet();
            //    ds.ReadXml(xmlreader);
            //    xmlreader.Close();
            //    //if ds is not empty  
            //    if (ds.Tables.Count != 0)
            //    {
            //        lblListofAllEmployee.Text = ds.Tables["EmployeeList"].Rows[0]["lblListofAllEmployee"].ToString();
            //        CheckBox1.Text = ds.Tables["EmployeeList"].Rows[0]["SelectAllUn-SelectALL"].ToString();
            //        BtnAdd.Text = ds.Tables["EmployeeList"].Rows[0]["cmdadd"].ToString();
            //        BtnDel.Text = ds.Tables["EmployeeList"].Rows[0]["cmddelete"].ToString();
            //        BtnImportFromExcel.Text = ds.Tables["EmployeeList"].Rows[0]["ImportfromExcel"].ToString();
            //        Button1.Text = ds.Tables["EmployeeList"].Rows[0]["cmdclose"].ToString();
            //        Button2.Text = ds.Tables["EmployeeList"].Rows[0]["cmdexportexcel"].ToString();
            //        Grid1.Columns[0].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns0"].ToString();
            //        Grid1.Columns[1].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns1"].ToString();
            //        Grid1.Columns[2].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns2"].ToString();
            //        Grid1.Columns[3].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns3"].ToString();
            //        Grid1.Columns[4].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns4"].ToString();
            //        Grid1.Columns[5].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns5"].ToString();
            //        Grid1.Columns[6].HeaderText = ds.Tables["EmployeeList"].Rows[0]["Grid1.Columns6"].ToString();
            //        //Grid1.Columns[7].HeaderText = "Branch";
            //    }
            //}
            //if (Session["language"] == "Spanish")
            //{
            //    lblListofAllEmployee.Text = "Lista de todos los empleados";
            //    CheckBox1.Text = "Seleccionar todo / Des-seleccionar TODO";
            //    BtnAdd.Text = "Añadir";
            //    BtnDel.Text = "Borrar";
            //    BtnImportFromExcel.Text = "Importar desde Excel";
            //    Button1.Text = "Cerca";
            //    Button2.Text = "Exportar Excel";
            //    Grid1.Columns[0].HeaderText = "Seleccionar";
            //    Grid1.Columns[1].HeaderText = "Nombre de empleado";
            //    Grid1.Columns[2].HeaderText = "Nro de tarjeta.";
            //    Grid1.Columns[3].HeaderText = "Emp. Código";
            //    Grid1.Columns[4].HeaderText = "Nombre del departamento";
            //    Grid1.Columns[5].HeaderText = "Denominación Nombre";
            //    Grid1.Columns[6].HeaderText = "Contraseña";
            // //   Grid1.Columns[7].HeaderText = "Nombre de la rama";
            //}

            //if (Session["language"] == "Chiness")
            //{
            //    lblListofAllEmployee.Text = "所有職員列表";
            //    CheckBox1.Text = "全部選取/全部反選";
            //    BtnAdd.Text = "新增";
            //    BtnDel.Text = "刪除";
            //    BtnImportFromExcel.Text = "從Excel導入";
            //    Button1.Text = "關閉";
            //    Button2.Text = "Export Excel";
            //    Grid1.Columns[0].HeaderText = "選取";
            //    Grid1.Columns[1].HeaderText = "員工姓名";
            //    Grid1.Columns[2].HeaderText = "卡號 (8位數設備用戶ID)";
            //    Grid1.Columns[3].HeaderText = "職員編號";
            //    Grid1.Columns[4].HeaderText = "部門名稱";
            //    Grid1.Columns[5].HeaderText = "職位名稱";
            //    Grid1.Columns[6].HeaderText = "密碼";
            //    //Grid1.Columns[7].HeaderText = "分店名稱";
            //}
            //else if (Session["language"] == "Thai")
            //{
            //    lblListofAllEmployee.Text = "พนักงานทั้งหมด";
            //    CheckBox1.Text = "เลือกทั้งหมด/เลือกออกทั้งหมด";
            //    BtnAdd.Text = "เพิ่ม";
            //    BtnDel.Text = "ลบ";
            //    BtnImportFromExcel.Text = "นำเข้าจาก Excel";
            //    Button1.Text = "ปิด";
            //    Button2.Text = "ส่งออก Excel";
            //    Grid1.Columns[0].HeaderText = "เลือก";
            //    Grid1.Columns[1].HeaderText = "ชื่อพนักงาน";
            //    Grid1.Columns[2].HeaderText = "หมายเลขบัตร.";
            //    Grid1.Columns[3].HeaderText = "รหัสพนักงาน";
            //    Grid1.Columns[4].HeaderText = "แผนก";
            //    Grid1.Columns[5].HeaderText = "ตำแหน่ง";
            //    Grid1.Columns[6].HeaderText = "รหัสผ่าน";
            //   // Grid1.Columns[7].HeaderText = "สาขา";
            //}
            //else if (Session["language"] == "Persian")
            //{
            //    lblListofAllEmployee.Text = "لیست تمام کارکنان";
            //    CheckBox1.Text = "انتخاب / عدم انتخاب همه";
            //    BtnAdd.Text = "اضافه";
            //    BtnDel.Text = "حذف";
            //    BtnImportFromExcel.Text = "دریافت از اکسل";
            //    Button1.Text = "بستن";
            //    Button2.Text = "صادرات اکسل";
            //    Grid1.Columns[0].HeaderText = "انتخاب";
            //    Grid1.Columns[1].HeaderText = "نام کارمند";
            //    Grid1.Columns[2].HeaderText = "شماره کارت";
            //    Grid1.Columns[3].HeaderText = "کد کارمندی";
            //    Grid1.Columns[4].HeaderText = "نام دپارتمان";
            //    Grid1.Columns[5].HeaderText = "عنوان شغلی";
            //    Grid1.Columns[6].HeaderText = "رمز عبور";
            //   // Grid1.Columns[7].HeaderText = "شعبه";
            //}

            //else if (Session["language"] == "Arabic")
            //{
            //    lblListofAllEmployee.Text = "قائمة كل الموظفين ";
            //    CheckBox1.Text = "اختر - لا تختر الجميع";
            //    BtnAdd.Text = "أضف";
            //    BtnDel.Text = "حذف";
            //    BtnImportFromExcel.Text = "الاستيراد من إكسل";
            //    Button1.Text = "أغلق";
            //    //  Button1.Text = "close";
            //    Button2.Text = "تصدير إكسيل";
            //    Grid1.Columns[0].HeaderText = "اختيار "; 
            //    Grid1.Columns[1].HeaderText = "اسم الموظف";
            //    Grid1.Columns[2].HeaderText = "رقم البطاقة";
            //    Grid1.Columns[3].HeaderText = "الرمز(الكود) الخاص بالموظف";
            //    Grid1.Columns[4].HeaderText = "اسم القسم ";
            //    Grid1.Columns[5].HeaderText = "اسم التكليف ";
            //    Grid1.Columns[6].HeaderText = "كلمة السر ";
            //    //Grid1.Columns[7].HeaderText = "فرع ";
            //}
            //else if (Session["language"] == "Turkish")
            //{
            //    lblListofAllEmployee.Text = "Tum calisanlar listesi";
            //    CheckBox1.Text = "Tümünü Seç / TÜMÜNÜ SEÇMEYİN";
            //    BtnAdd.Text = "Ekle";
            //    BtnDel.Text = "Sil";
            //    BtnImportFromExcel.Text = "Excel'den içe aktarma";
            //    Button1.Text = "Kapat";
            //    Button2.Text = "Excel'i dışa aktar";
            //    Grid1.Columns[0].HeaderText = "Secim";
            //    Grid1.Columns[1].HeaderText = "Calisan ismi";
            //    Grid1.Columns[2].HeaderText = "Kart no (sadece sekiz dijit)";
            //    Grid1.Columns[3].HeaderText = "Calisan kodu";
            //    Grid1.Columns[4].HeaderText = "Departman ismi";
            //    Grid1.Columns[5].HeaderText = "Atama ismi";
            //    Grid1.Columns[6].HeaderText = "Sifre";
            //  //  Grid1.Columns[7].HeaderText = "Dal";
            //}



            this.Title = "Realtime-Employee List";
            if (Session["uid"] == null)
            {
                Response.Redirect("Default.aspx");
                return;

            }
            else
            {
                // checklic();

                try
                {
                    Database.ExecuteText(HttpContext.Current.Session["con"].ToString(), "Alter TAble Mst_employee Add ProfilePic Image ");
                }


                catch (Exception eee) { }
                Binddata();
                CheckRights();


            }

        }
    }

    void CheckRights()
    {

        DataTable dt = Database.ExecuteTextDataTable(HttpContext.Current.Session["con"].ToString(), "Select * from  Mst_user Where  User_Id=" + Session["uid"].ToString().Trim() + "");
        if (dt.Rows.Count > 0)
        {

            string a = "";
            int b = 8;
            bool result = false;
            foreach (object obj in dt.Rows[0]["per"].ToString())
            {
                if (obj.ToString() == ",")
                {
                    if (b == Convert.ToInt32(a))
                    {
                        result = true;
                    }

                    a = "";

                }
                else
                {
                    a = a + obj.ToString().Trim();
                }

            }


            if (result == true)
            {
            }
            else
            {

                Response.Redirect("welcome.aspx");
            }
        }


    }

    protected void Grid1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Grid1.PageIndex = e.NewPageIndex;
        Binddata();
    }
    public void Binddata()
    {

        string inqry = "";
        if (Session["Branch_per"] != null)
        {
            inqry = Session["Branch_per"].ToString();
            int aa = inqry.Length;
            if (inqry.Length == 0)
            {
                inqry = "0";
            }
        }
        else
        {
            Session["Branch_per"] = "0";
        }


        string qry2 = "";
        if (Request.QueryString["searchemp"] != null)
        {
            qry2 = Request.QueryString["searchemp"].ToString();


            string ss = "SELECT [EmpId],[EmpName] as EmpName ,[Cardno],[EmpCode],[Dept_Name],[Desig_Name],pwd  as [DateofJoin],d.Branch_Name as Branch  FROM dbo.mst_employee a,dbo.[mst_DEpartment] b, dbo.[mst_designation] c,dbo.mst_branch d    where a.[DeptId]=b.[DeptId] and a.branchId=d.branchId and a.DesigId=c.DesigId and a.branchId in(" + inqry + ")  and empname like '%" + qry2 + "%'  Order by Branch_NAme,Empcode";


            //DataTable dt = BusinessObject.Selectmst_employeeAll();
            DataTable dt = Database.ExecuteTextDataTable(HttpContext.Current.Session["con"].ToString(), ss);
            if (dt.Rows.Count > 0)
            {
                //btnDelete.Visible = true;
                Grid1.DataSource = dt;
                Grid1.DataBind();

            }
            else
            {
                dt = null;
                Grid1.DataSource = dt;
                Grid1.DataBind();
                Grid1.EmptyDataText = "There is no record";
                //btnDelete.Visible = false;
                return;
            }



        }
        else
        {
            string ss = "";
            if( RadioButton3.Checked==true)
            {
                // all employee
                ss = "SELECT [EmpId],[EmpName]  as EmpName,[Cardno],[EmpCode],[Dept_Name],[Desig_Name],pwd  as [DateofJoin],d.Branch_Name as Branch  FROM dbo.mst_employee a,dbo.[mst_DEpartment] b, dbo.[mst_designation] c,dbo.mst_branch d    where a.[DeptId]=b.[DeptId] and a.branchId=d.branchId and a.DesigId=c.DesigId and a.branchId in(" + inqry + ")  Order by Branch_NAme,Empcode";
            }
            if (RadioButton1.Checked == true)
            {
                // active
                ss = "SELECT [EmpId],[EmpName] as  EmpName,[Cardno],[EmpCode],[Dept_Name],[Desig_Name],pwd  as [DateofJoin],d.Branch_Name as Branch  FROM dbo.mst_employee a,dbo.[mst_DEpartment] b, dbo.[mst_designation] c,dbo.mst_branch d    where a.[DeptId]=b.[DeptId] and a.branchId=d.branchId and a.DesigId=c.DesigId and a.branchId in(" + inqry + ")  and len(ReginDate)<6 Order by Branch_NAme,Empcode";
            }
            if (RadioButton2.Checked == true)
            {
                // in active
                ss = "SELECT [EmpId],[EmpName] as EmpName,[Cardno],[EmpCode],[Dept_Name],[Desig_Name],pwd  as [DateofJoin],d.Branch_Name as Branch  FROM dbo.mst_employee a,dbo.[mst_DEpartment] b, dbo.[mst_designation] c,dbo.mst_branch d    where a.[DeptId]=b.[DeptId] and a.branchId=d.branchId and a.DesigId=c.DesigId and a.branchId in(" + inqry + ") and  len(ReginDate)>6  Order by Branch_NAme,Empcode";
            }

            //DataTable dt = BusinessObject.Selectmst_employeeAll();
            DataTable dt = Database.ExecuteTextDataTable(HttpContext.Current.Session["con"].ToString(), ss);
            if (dt.Rows.Count > 0)
            {
                //btnDelete.Visible = true;
                Grid1.DataSource = dt;
                Grid1.DataBind();

            }
            else
            {
                dt = null;
                Grid1.DataSource = dt;
                Grid1.DataBind();
                Grid1.EmptyDataText = "There is no record";
                //btnDelete.Visible = false;
                return;
            }
        }
    }
    protected void BtnDel_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gdv in Grid1.Rows)
        {
            CheckBox chk = (CheckBox)gdv.FindControl("cbRows");
            if (chk.Checked == true)
            {
                int nstockid = Convert.ToInt32(Grid1.DataKeys[gdv.RowIndex].Value);
                int pd;
                pd = BusinessObject.Deletemst_employee(nstockid);
                if (pd == 0)
                {

                    Database.ExecuteText(HttpContext.Current.Session["con"].ToString(), "Delete from tran_Attendance Where empid=" + nstockid + "");
                    //lblMessage.Text = "This Record Deleted Successfully";
                }
                else if (pd == 1)
                {
                    //lblMessage.Text = "Some record are used can not be deleted.";
                }

            }
        }
        Binddata();
    }
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("Employee.aspx?RowId=0");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Welcome.aspx");
    }

    protected void BtnImportFromExcel_Click(object sender, EventArgs e)
    {
        Response.Redirect("UploadTemplate.aspx");
    }
    protected void Grid1_DataBound(object sender, EventArgs e)
    {
        try
        {
            GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
            for (int i = 0; i < Grid1.Columns.Count; i++)
            {
                TableHeaderCell cell = new TableHeaderCell();
                TextBox txtSearch = new TextBox();
                CheckBox chkall = new CheckBox();
                Label txtSearch1 = new Label();
                if (i == 0)
                {
                    chkall.ID = "chkallop";
                    cell.Controls.Add(chkall);
                    row.Controls.Add(cell);
                }
                if (i == 0 || i == 6 || i == 8)
                {
                    txtSearch1.Attributes["placeholder"] = "";
                    txtSearch1.Width = 30;
                    txtSearch1.CssClass = "search_textbox";
                    cell.Controls.Add(txtSearch1);
                    row.Controls.Add(cell);

                }

                else if (i == 2 || i == 3)
                {
                    txtSearch.Attributes["placeholder"] = "";
                    txtSearch.Width = 90;
                    txtSearch.Height = 24;
                    txtSearch.CssClass = "search_textbox";
                    cell.Controls.Add(txtSearch);
                    row.Controls.Add(cell);
                }
                else if (i == 1 || i == 4 || i == 5 || i == 7)
                {
                    txtSearch.Attributes["placeholder"] = "";
                    txtSearch.Width = 140;
                    txtSearch.Height = 24;
                    txtSearch.CssClass = "search_textbox";
                    cell.Controls.Add(txtSearch);
                    row.Controls.Add(cell);
                }

            }
            Grid1.HeaderRow.Parent.Controls.AddAt(1, row);
        }

        catch (Exception e3) { }
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        try
        {

            foreach (GridViewRow row in Grid1.Rows)
            {

                if (row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox c = (CheckBox)row.FindControl("cbRows");
                    if (CheckBox1.Checked == true)
                    {
                        c.Checked = true;
                    }
                    else
                    {
                        c.Checked = false;
                    }


                }

            }
        }
        catch { }





    }

    protected void Button2_Click(object sender, EventArgs e)
    {

        ////BY Dharmendra-04/11/2017
        string csv = string.Empty;
        string inqry = "";
        if (Session["Branch_per"] != null)
        {
            inqry = Session["Branch_per"].ToString();
            int aa = inqry.Length;
            if (inqry.Length == 0)
            {
                inqry = "0";
            }
        }
        else
        {
            Session["Branch_per"] = "0";
        }

        string ss = "SELECT [EmpName],[Cardno],[EmpCode],[Dept_Name],[Desig_Name],d.Branch_Name as Branch  FROM dbo.mst_employee a,dbo.[mst_DEpartment] b, dbo.[mst_designation] c,dbo.mst_branch d    where a.[DeptId]=b.[DeptId] and a.branchId=d.branchId and a.DesigId=c.DesigId and a.branchId in(" + inqry + ")  Order by Branch_NAme,Empcode";


        DataTable dt = Database.ExecuteTextDataTable(HttpContext.Current.Session["con"].ToString(), ss);

        string str = "Employee Name,Card No.,Employee Code,Department Name,Designation Name,Branch,";
        string[] strdpl = str.Split(',');
        foreach (string i in strdpl)
        {
            csv += i + ',';
        }
        csv += "\r\n";

        if (dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn column in dt.Columns)
                {
                    //Add the Data rows.
                    csv += row[column.ColumnName].ToString().Replace(",", ";") + ',';
                }
                //Add new line.    
                csv += "\r\n";
            }

        }

        ///    csv += "\r\n";

        //  Download the CSV file.

        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=TotalEmployee.csv");
        Response.Charset = "";
        Response.ContentType = "application/text";
        Response.Output.Write(csv);
        Response.Flush();
        Response.End();


    }
    protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
    {
        Binddata();
    }
    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        Binddata();
    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        Binddata();
    }
}
